#include "../../mqServer/route.hpp"
#include <gtest/gtest.h>

using namespace mq;

class RouteTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cout << "所有单元测试前执行环境初始化" << endl;
    }
    virtual void TearDown() override
    {
        cout << "所有单元测试后执行环境清理" << endl;
    }
};

TEST(route_test, str_test)
{
    // 合法性测试
    ASSERT_EQ(Router::route(DIRECT, "abc&%", "abc.c"), false);
    ASSERT_EQ(Router::route(DIRECT, "abc.c", "abc.c&^"), false);
    ASSERT_EQ(Router::route(DIRECT, "abc.#.", "abc.c"), false);
    ASSERT_EQ(Router::route(DIRECT, "abc", "abc.c#"), false);
    ASSERT_EQ(Router::route(DIRECT, "abc", "abc.#.*"), false);

    // 直接交换
    ASSERT_EQ(Router::route(DIRECT, "abc", "abc"), true);
    ASSERT_EQ(Router::route(DIRECT, "abc.dd", "abc.dd"), true);
    ASSERT_EQ(Router::route(DIRECT, "abc", "abc.#"), false);
    ASSERT_EQ(Router::route(DIRECT, "abc.#", "abc.#"), false);

    // 广播交换
    ASSERT_EQ(Router::route(FANOUT, "abc", "abc"), true);
    ASSERT_EQ(Router::route(FANOUT, "abc.cc", "abc"), true);
    ASSERT_EQ(Router::route(FANOUT, "abc.cc", "abc.#"), true);
    ASSERT_EQ(Router::route(FANOUT, "abc.cc", "abc.#.*"), false);

    // 主题交换
    ASSERT_EQ(Router::route(TOPIC, "abc", "abc"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc.dd", "abc.dd"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc", "abc.*"), false);
    ASSERT_EQ(Router::route(TOPIC, "b.abc", "*.abc"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc.d.c", "abc.*.c"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc", "abc.#"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc", "#.abc"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc", "c.#.abc"), false);
    ASSERT_EQ(Router::route(TOPIC, "abc.a.c.ss", "abc.#"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc.sa.sa.d", "abc.#.d"), true);
    ASSERT_EQ(Router::route(TOPIC, "abc.sa.sa.d.s.dsa.s.sa.b", "abc.#.d.*.*.s.#.b.#"), true);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new RouteTest);
    RUN_ALL_TESTS();
    return 0;
}